package com.miot.restapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.miot.domain.util.MIHomeUtil;
import com.miot.object.common.QueryControlIdVo;
import com.miot.object.home.SaveHomeVo;
import com.miot.object.home.SaveRoomVo;
import com.miot.restapi.util.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;

/**
 * @Author: zhangyanbin
 * @Date: 2019/1/30 15:13
 * @Version 1.0
 */
@RestController
@RequestMapping("home")
@Api(value = "home", description = "小房间控制器")
public class MiotHomeRestController {

    protected static Logger logger = LoggerFactory.getLogger(MiotHomeRestController.class);

    /**
     * 手机端-创建家庭
     *
     * @param homeVo
     * @return
     */
    @PostMapping("/createHome")
    @ApiOperation(value = "创建家庭", notes = "创建家庭", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public CompletableFuture<DataResult> createHome(@RequestBody SaveHomeVo homeVo) throws Exception {
        return CompletableFuture.supplyAsync(() -> {
            logger.info("手机端-创建家庭->" + homeVo);
            JSONObject home = MIHomeUtil.createHome(homeVo.getControlId(), homeVo.getHomeName());
            return DataResult.ok(home);
        });
    }

    /**
     * 手机端-创建小房间
     *
     * @param roomVo
     * @return
     */
    @PostMapping("/createRome")
    @ApiOperation(value = "创建小房间", notes = "创建小房间", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public CompletableFuture<DataResult> createRome(@RequestBody SaveRoomVo roomVo) {
        return CompletableFuture.supplyAsync(() -> {
            logger.info("手机端-创建小房间->" + roomVo);
            JSONObject home = MIHomeUtil.createRoom(roomVo.getControlId(), roomVo.getHomeId(), roomVo.getRoomName());
            return DataResult.ok(home);
        });
    }

    /**
     * 手机端-查询小房间列表
     *
     * @param controlIdVo
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "查询小房间列表", notes = "查询小房间列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public CompletableFuture<DataResult> list(@RequestBody QueryControlIdVo controlIdVo) {
        return CompletableFuture.supplyAsync(() -> {
            logger.info("手机端-创建小房间->" + controlIdVo);
            JSONObject home = MIHomeUtil.getHome(controlIdVo.getControlId());
            JSONArray roomList = home.getJSONArray("homeInfo").getJSONObject(0).getJSONArray("roomlist");
            return DataResult.ok(roomList);
        });
    }



}
